import request from "./useRequests";
import { ElMessage } from 'element-plus'
import getComments from '@/api/getComments'
import { userinfo } from '@/stores/counter'
const user = userinfo()

export default function addComments() {
  const { comments } = getComments();
  async function commenting(articleId: any, content: string, commentId:number = 0, fatherId: number = 0
    ,reply_commentId: number = 0) {
    if (!content) {
      ElMessage({
        showClose: true,
        message: '不能输入空字符',
        type: 'error',
      })
      return 0
    }
    try {
      console.log(user.baseInfo.token)
      const res = await request.post('/comment', {
        "articleId": articleId,
        "fatherId": fatherId,
        "content": content,
        "commentId": commentId,
        "replyCommentId": reply_commentId
      },{
        headers: {
          authentication: user.baseInfo.token
        },
      })
      // console.log(res)
      if (res.data.status == true) {
        ElMessage({
          showClose: true,
          message: '评论成功',
          type: 'success',
        })
        setTimeout(() => {
          comments(articleId)
        }, 1000);
      } else {
        ElMessage({
          showClose: true,
          message: `${res.data.message}`,
          type: 'error',
        })
        // console.log(res)
        return 0
      }
    } catch (err: any) {
      ElMessage({
        showClose: true,
        message: `评论失败`,
        type: 'error',
      })
      //alert(err)
    }
    return 1
  }

  return {
    commenting
  }
}